﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.SaoLuu
{
    public partial class frmQuanLy : Form
    {
        public frmQuanLy()
        {
            InitializeComponent();
        }

        private void frmSaoLuu_Load(object sender, EventArgs e)
        {
            Select();
        }
        Entities.SaoLuuHeThong[] sl;
        #region Select
        private void Select()
        {
            sl = Common.StaticVariable.InterfaceObject.SelectSaoLuu();
            if (sl == null)
            {
                sl = new Entities.SaoLuuHeThong[0];
            }
            dgvHienThi.DataSource = sl;
            dgvHienThi.Columns[0].Visible = false;
            dgvHienThi.Columns[1].HeaderText = "Tên";
            dgvHienThi.Columns[2].HeaderText = "Thời gian";
            dgvHienThi.Columns[3].HeaderText = "Dung lượng";
        }
        #endregion
        #region Restore
        private bool Restore(Entities.SaoLuuHeThong sl)
        {
            return Common.StaticVariable.InterfaceObject.Restore(sl);
        }
        #endregion

        private void btnRestore_Click(object sender, EventArgs e)
        {
            if (dgvHienThi.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Bạn muốn thực hiện thao tác này?", "Thống báo", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    Entities.SaoLuuHeThong sl = (Entities.SaoLuuHeThong)dgvHienThi.SelectedRows[0].DataBoundItem;
                    bool tv = Restore(sl);
                    if (tv)
                    {
                        MessageBox.Show("Thành công", "Thông báo");
                    }
                    else
                    {
                        MessageBox.Show("Thất bại", "thông báo");
                    }
                }
            }
        }

        
        #region text change
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            Entities.SaoLuuHeThong[] temp;
            temp = sl.Where(s => s.Name.ToLower().Contains(txtTimKiem.Text.ToLower())).ToArray<Entities.SaoLuuHeThong>();
            if(temp!=null )
                dgvHienThi.DataSource = temp;
            else
                dgvHienThi.DataSource=new Entities.SaoLuuHeThong[0];
            
        }
        #endregion

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBackUp_Click(object sender, EventArgs e)
        {
            bool b = Common.StaticVariable.InterfaceObject.BackUp();
            if (b)
            {
                MessageBox.Show("Back Up Thành Công");
                Select();
            }
            else
            {
                MessageBox.Show("Back Up Thất Bại");
            }
        }

    }
}
